import { createWebHistory, createRouter } from "vue-router";
import { microAppBaseRouter, microAppForceSendData } from "../microApp/utils";

const Layout = () => import(/** layout */ "@/layout");

const ErrorPage = () => import(/** error */ "@/views/error/404.vue");

const ContractList = () => import(/** contractList */ "@/views/contractList/index.vue");

const ContractsProvided = () => import(/** contractsProvided */ "@/views/contractsProvided/index.vue");

// 公共路由
export const constantRoutes = [
  {
    path: "/:pathMatch(.*)*",
    component: ErrorPage,
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/contract-list",
    children: [
      {
        path: "/contract-list",
        component: ContractList,
        name: "contractList",
        meta: { title: "销售合同管理", keepAlive: true }
      },
      {
        path: "/contracts-provided",
        component: ContractsProvided,
        name: "contractsProvided",
        meta: { title: "生成合同文件" }
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(microAppBaseRouter()),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

router.beforeEach(async (to, from, next) => {
  microAppForceSendData(to);
  next();
});

export default router;
